package cn.iocoder.yudao.module.shop.controller.admin.priceitem;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.priceitem.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.priceitem.ShopPriceItemDO;
import cn.iocoder.yudao.module.shop.convert.priceitem.ShopPriceItemConvert;
import cn.iocoder.yudao.module.shop.service.priceitem.ShopPriceItemService;

@Tag(name = "管理后台 - 价格单项")
@RestController
@RequestMapping("/shop/price-item")
@Validated
public class ShopPriceItemController {

    @Resource
    private ShopPriceItemService shopPriceItemService;

    @PostMapping("/create")
    @Operation(summary = "创建价格单项")
    @PreAuthorize("@ss.hasPermission('shop:price-item:create')")
    public CommonResult<Long> createPriceItem(@Valid @RequestBody ShopPriceItemCreateReqVO createReqVO) {
        return success(shopPriceItemService.createPriceItem(createReqVO));
    }

    @PostMapping("/create/batch")
    @Operation(summary = "创建多个价格单项")
    @PreAuthorize("@ss.hasPermission('shop:price-item:create')")
    public CommonResult<Boolean> createPriceItemBatch(@Valid @RequestBody ShopPriceItemBatchCreateReqVO createReqVO) {
        return success(shopPriceItemService.createPriceItemBatch(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新价格单项")
    @PreAuthorize("@ss.hasPermission('shop:price-item:update')")
    public CommonResult<Boolean> updatePriceItem(@Valid @RequestBody ShopPriceItemUpdateReqVO updateReqVO) {
        shopPriceItemService.updatePriceItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除价格单项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:price-item:delete')")
    public CommonResult<Boolean> deletePriceItem(@RequestParam("id") Long id) {
        shopPriceItemService.deletePriceItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得价格单项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:price-item:query')")
    public CommonResult<ShopPriceItemRespVO> getPriceItem(@RequestParam("id") Long id) {
        ShopPriceItemDO priceItem = shopPriceItemService.getPriceItem(id);
        return success(ShopPriceItemConvert.INSTANCE.convert(priceItem));
    }

    @GetMapping("/list")
    @Operation(summary = "获得价格单项列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:price-item:query')")
    public CommonResult<List<ShopPriceItemRespVO>> getPriceItemList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopPriceItemDO> list = shopPriceItemService.getPriceItemList(ids);
        return success(ShopPriceItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得价格单项分页")
    @PreAuthorize("@ss.hasPermission('shop:price-item:query')")
    public CommonResult<PageResult<ShopPriceItemRespVO>> getPriceItemPage(@Valid ShopPriceItemPageReqVO pageVO) {
        PageResult<ShopPriceItemDO> pageResult = shopPriceItemService.getPriceItemPage(pageVO);
        return success(ShopPriceItemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出价格单项 Excel")
    @PreAuthorize("@ss.hasPermission('shop:price-item:export')")
    @OperateLog(type = EXPORT)
    public void exportPriceItemExcel(@Valid ShopPriceItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopPriceItemDO> list = shopPriceItemService.getPriceItemList(exportReqVO);
        // 导出 Excel
        List<ShopPriceItemExcelVO> datas = ShopPriceItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "价格单项.xls", "数据", ShopPriceItemExcelVO.class, datas);
    }

}
